<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>Rxjava-简化你的异步调用 | 清风向阳</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
    <meta name="keywords" content="Joker,Joker's Blog" />
  
  <meta name="description" content="Rxjava好似一股春风，刮到了各地的论坛，博客，其实Rxjava给我们带来的是一个对编程思维的改变，它改变了我们对异步回调的正常模式，主张链式是最具表达力的方式，方便阅读，代码清晰，调理清楚。我们传统的回调都是采用接口的模式，而Rxjava给我们带来了新的体验，那么我们就好领略下它的魅力吧。">
<meta name="keywords" content="Rxjava">
<meta property="og:type" content="article">
<meta property="og:title" content="Rxjava-简化你的异步调用">
<meta property="og:url" content="http://mjjde.github.io/2015/12/14/Rxjava-简化你的异步调用/index.html">
<meta property="og:site_name" content="清风向阳">
<meta property="og:description" content="Rxjava好似一股春风，刮到了各地的论坛，博客，其实Rxjava给我们带来的是一个对编程思维的改变，它改变了我们对异步回调的正常模式，主张链式是最具表达力的方式，方便阅读，代码清晰，调理清楚。我们传统的回调都是采用接口的模式，而Rxjava给我们带来了新的体验，那么我们就好领略下它的魅力吧。">
<meta property="og:updated_time" content="2017-09-15T07:00:35.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Rxjava-简化你的异步调用">
<meta name="twitter:description" content="Rxjava好似一股春风，刮到了各地的论坛，博客，其实Rxjava给我们带来的是一个对编程思维的改变，它改变了我们对异步回调的正常模式，主张链式是最具表达力的方式，方便阅读，代码清晰，调理清楚。我们传统的回调都是采用接口的模式，而Rxjava给我们带来了新的体验，那么我们就好领略下它的魅力吧。">
  
  
    <link rel="icon" href="/favicon_pic.ico">
  
  <link href="//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="/css/style.css">
  <script src="/js/pace.min.js"></script>
  

  
  

</head>

<body>
  <div id="container">
      <header id="header">
    <div id="banner"></div>
    <div id="header-outer">
        <div id="header-menu" class="header-menu-pos animated">
            <div class="header-menu-container">
                <a href="/" class="left">
                    <span class="site-title">Joker&#39;s Blog</span>
                </a>
                <nav id="header-menu-nav" class="right">
                    
                    <a  href="/">
                        <i class="fa fa-home"></i>
                        <span>Home</span>
                    </a>
                    
                    <a  href="/archives">
                        <i class="fa fa-archive"></i>
                        <span>Archives</span>
                    </a>
                    
                    <a  href="/about">
                        <i class="fa fa-user"></i>
                        <span>About</span>
                    </a>
                    
                </nav>
                <a class="mobile-header-menu-button">
                    <i class="fa fa-bars"></i>
                </a>
            </div>
        </div>
        <div id="header-row">
            <div id="logo">
                <a href="/">
                    <img src="/images/avatar.jpg" alt="logo">
                </a>
            </div>
            <div class="header-info">
                <div id="header-title">
                    
                    <h2>
                        Joker&#39;s Blog
                    </h2>
                    
                </div>
                <div id="header-description">
                    
                    <h3>
                        一个认真的技术博客
                    </h3>
                    
                </div>
            </div>
            <nav class="header-nav">
                <div class="social">
                    
                        <a title="Joker"  href="//joker-studio.pub">
                            <i class="fa fa-home fa-2x"></i></a>
                    
                        <a title="Github" target="_blank" href="//github.com/mjjde">
                            <i class="fa fa-github fa-2x"></i></a>
                    
                </div>
            </nav>
        </div>
    </div>
</header>
      <div class="outer">
        <section id="main" class="body-wrap"><article id="post-Rxjava-简化你的异步调用" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="post-title" itemprop="name">
      Rxjava-简化你的异步调用
    </h1>
    <div class="post-title-bar">
      <ul>
          
              <li>
                  <i class="fa fa-book"></i>
                  
                      <a href="/categories/Android技术/">Android技术</a>
                  
              </li>
          
        <li>
          <i class="fa fa-calendar"></i>  2015-12-14
        </li>
        <li>
          <i class="fa fa-eye"></i>
          <span id="busuanzi_value_page_pv"></span>
        </li>
      </ul>
    </div>
  

          
      </header>
    
    <div class="article-entry post-content" itemprop="articleBody">
      
            
            <blockquote>
<p>Rxjava好似一股春风，刮到了各地的论坛，博客，其实Rxjava给我们带来的是一个对编程思维的改变，它改变了我们对异步回调的正常模式，主张链式是最具表达力的方式，方便阅读，代码清晰，调理清楚。我们传统的回调都是采用接口的模式，而Rxjava给我们带来了新的体验，那么我们就好领略下它的魅力吧。<br><a id="more"></a><br>Rxjava的GitHub<a href="https://github.com/ReactiveX" target="_blank" rel="external">地址</a></p>
</blockquote>
<p>Rxjava的doc<a href="http://reactivex.io/RxJava/javadoc" target="_blank" rel="external">地址</a></p>
<h4 id="Rxjava带给我们什么"><a href="#Rxjava带给我们什么" class="headerlink" title="Rxjava带给我们什么"></a><a href="#Rxjava_u5E26_u7ED9_u6211_u4EEC_u4EC0_u4E48" title="Rxjava带给我们什么"></a>Rxjava带给我们什么</h4><p>Rxjava到底给我们带来的是什么？我们引用Rxjava自己的解释：a library for composing asynchronous and event-based programs by using observable sequences.是一个用来编写异步的或基于事件的程序的一个库。这句话也阐述了它的具体作用。</p>
<h4 id="如何将添加Rxjava到项目中"><a href="#如何将添加Rxjava到项目中" class="headerlink" title="如何将添加Rxjava到项目中"></a><a href="#u5982_u4F55_u5C06_u6DFB_u52A0Rxjava_u5230_u9879_u76EE_u4E2D" title="如何将添加Rxjava到项目中"></a>如何将添加Rxjava到项目中</h4><p>我么只需要在gradle中添加 compile ‘io.reactivex:rxjava:1.0.14’</p>
<p>作为Android 开发 我们还可以添加为Android定制的RxAndroid库 compile ‘io.reactivex:rxandroid:1.0.1’</p>
<p>这样我们的项目已经可以使用Rxjava了。</p>
<h4 id="Rxjava的思想"><a href="#Rxjava的思想" class="headerlink" title="Rxjava的思想"></a><a href="#Rxjava_u7684_u601D_u60F3" title="Rxjava的思想"></a>Rxjava的思想</h4><p>Rxjava是对观察者模式的一种扩展，我们Android在开发的时候也会经常用到观察者模式，比如最简单的点击事件，我们都会注册一个接口Listener给组件对象，只要当触发了点击事件时，组件会判断是否设置了Listener,如果不为空，那么就调用Listener接口的方法。显然观察者模式在我们Android系统里是普遍存在的，那么我们是不是觉得这样写起来对于编写者来说可能会清晰的，可以接受的，但是作为一个阅读者来说，并不是那么直观了，因为涉及到多个类的调用，我们会去在代码中跳来跳去，去寻找来龙去脉。<br>既然Rxjava也是基于观察者模式，那么必然也会有该模式所需要的元素：被观察者Observable,观察者Observer，注册的动作(订阅)subscribe，还有就是触发的事件源。这样就构成了一个完整的事件链。<br>上面说到传统的观察者和被观察这都会实现在不同的文件下面，那么给阅读者带了不小的麻烦，那么使用Rxjava就避免了这样的问题，我们可以在一个地方去写出被观察者和观察者，同时Rxjava还给我们来带了2个不错的作用点。也是观察者自带的两个回调方法，onCompleted和onError，分别表示被观察的动作队列全部完成了，第二个是当事件发生错误时的调用，这两个机制是我们传统的代码所不具备的，好处就不言而喻了最直观的是我们拥有了2个额外的回调，可以出来我们额外处理的事情，提高了代码的健壮性。</p>
<h4 id="Rxjava的使用"><a href="#Rxjava的使用" class="headerlink" title="Rxjava的使用"></a><a href="#Rxjava_u7684_u4F7F_u7528" title="Rxjava的使用"></a>Rxjava的使用</h4><p>在我们上面说过的观察者模式所需的元素，我们主要实现如下：</p>
<h5 id="观察者Observer"><a href="#观察者Observer" class="headerlink" title="观察者Observer"></a><a href="#u89C2_u5BDF_u8005Observer" title="观察者Observer"></a>观察者Observer</h5><p>在Rxjava中有两种创建方式</p>
<p>Observe的创建有两种：</p>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line">Observer&lt;T&gt; observer = <span class="keyword">new</span> Observer&lt;T&gt;() &#123;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">onCompleted</span><span class="params">()</span> </span>&#123;</div><div class="line"></div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable e)</span> </span>&#123;</div><div class="line"></div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">onNext</span><span class="params">(T t)</span> </span>&#123;</div><div class="line"></div><div class="line">    &#125;</div><div class="line">&#125;;                                                                                                                </div><div class="line"></div><div class="line">Subscriber&lt;String&gt; subscriber = <span class="keyword">new</span> Subscriber() &#123;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">onCompleted</span><span class="params">()</span> </span>&#123;</div><div class="line"></div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable e)</span> </span>&#123;</div><div class="line"></div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">onNext</span><span class="params">(Object o)</span> </span>&#123;</div><div class="line"></div><div class="line">    &#125;</div><div class="line">&#125;;</div></pre></td></tr></table></figure>
<p>我们在源码部分看到Observer是个接口，而Subscriber是个抽象类实现了Observer和Subscription<br>主要区别是Subscriber多了Subscription接口提供的unsubscribe()和isUnsubscribed()以及onStart()方法，对我们来说其他的都差不多</p>
<ul>
<li>onStart()方法是在处理动作队列值前调用的，主要目的是我们可以做一些初始化等处理。</li>
<li>unsubscribe()方法是用来取消订阅的，在取消之前我们可以调用isUnsubscribed（）判断是否已经取消订阅了，这个方法可以避免一些内存溢出的情况发生，我们可以在一个Activity的onStop()里调用该方法，就像Android自带的注册一样，使用的时候还是要注意下。</li>
</ul>
<h5 id="被观察者Observable"><a href="#被观察者Observable" class="headerlink" title="被观察者Observable"></a><a href="#u88AB_u89C2_u5BDF_u8005Observable" title="被观察者Observable"></a>被观察者Observable</h5><p>被观察者是用来处理动作并提醒观察者感兴趣的事件被触发了。Observable的创建只有一种方式</p>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">Observable&lt;T&gt; observable = Observable.create(<span class="keyword">new</span> Observable.OnSubscribe&lt;T&gt;() &#123;</div><div class="line">       <span class="meta">@Override</span></div><div class="line">       <span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">call</span><span class="params">(Subscriber&lt;? <span class="keyword">super</span> T&gt; subscriber)</span> </span>&#123;</div><div class="line">           subscriber.onNext();</div><div class="line">           subscriber.onCompleted();</div><div class="line">           subscriber.onError();</div><div class="line">       &#125;</div><div class="line">   &#125;)</div></pre></td></tr></table></figure>
<p>我们通过调用Observable的create方法来创建一个被观察者，并传入一个实现Observable.OnSubscribe的参数，在call()方法里面subscriber.onNext()定义我们要处理的事件,可以有多个subscriber.onNext()方法来制定事件列表，一旦被订阅后，定义好的事件将会一步一步的执行下去。上面的subscriber.onCompleted()和subscriber.onError()是互斥的，在具体的运行中只会运行其中的一个。上面的create方式创建的Observable算是自定义的形式，我们Rxjava还是提供了很多很多的Operators来给我们创建Observable，这个我们会在后面介绍。</p>
<h5 id="订阅subscribe"><a href="#订阅subscribe" class="headerlink" title="订阅subscribe"></a><a href="#u8BA2_u9605subscribe" title="订阅subscribe"></a>订阅subscribe</h5><p>我们创建好了Observe和Observable，我们要订阅将这两个联系起来：</p>
<figure class="highlight gcode"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">observable.subscribe<span class="comment">(observer)</span>;</div><div class="line"><span class="comment">//或者</span></div><div class="line">observable.subscribe<span class="comment">(subscriber)</span>;</div></pre></td></tr></table></figure>
<p>我们看下这两个的大致源码：</p>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="keyword">final</span> <span class="function">Subscription <span class="title">subscribe</span><span class="params">(<span class="keyword">final</span> Observer&lt;? <span class="keyword">super</span> T&gt; observer)</span> </span>&#123;</div><div class="line">    <span class="keyword">if</span> (observer <span class="keyword">instanceof</span> Subscriber) &#123;</div><div class="line">        <span class="keyword">return</span> subscribe((Subscriber&lt;? <span class="keyword">super</span> T&gt;)observer);</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">return</span> <span class="title">subscribe</span><span class="params">(<span class="keyword">new</span> Subscriber&lt;T&gt;()</span> </span>&#123;</div><div class="line"></div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">onCompleted</span><span class="params">()</span> </span>&#123;</div><div class="line">            observer.onCompleted();</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable e)</span> </span>&#123;</div><div class="line">            observer.onError(e);</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">onNext</span><span class="params">(T t)</span> </span>&#123;</div><div class="line">            observer.onNext(t);</div><div class="line">        &#125;</div><div class="line"></div><div class="line">    &#125;);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>我们可以看到其实调用的还是subscribe(subscriber);这里将Observer转换成了Subscriber。</p>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"> <span class="keyword">private</span> <span class="keyword">static</span> &lt;T&gt; <span class="function">Subscription <span class="title">subscribe</span><span class="params">(Subscriber&lt;? <span class="keyword">super</span> T&gt; subscriber, Observable&lt;T&gt; observable)</span> </span>&#123;</div><div class="line">        subscriber.onStart();</div><div class="line">        hook.onSubscribeStart(observable, observable.onSubscribe).call(subscriber);</div><div class="line">        <span class="function"><span class="keyword">return</span> hook.<span class="title">onSubscribeReturn</span><span class="params">(subscriber)</span></span>;</div><div class="line">        <span class="function"><span class="keyword">return</span> Subscriptions.<span class="title">unsubscribed</span><span class="params">()</span></span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>首先是调用了subscriber.onStart()方法，我们之前以说过了。hook.onSubscribeStart(observable, observable.onSubscribe)返回的就是参数里面observable.onSubscribe也就是自己subscriber。onSubscribe.call();</p>
<p>订阅的时候，Rxjava为我们提供了一些便捷的缺省操作。如</p>
<p>public final Subscription subscribe(final Action1&lt;? super T&gt; onNext)</p>
<p>public final Subscription subscribe(final Action1&lt;? super T&gt; onNext, final Action1 <throwable>onError)</throwable></p>
<p>public final Subscription subscribe(final Action1&lt;? super T&gt; onNext, final Action1 <throwable>onError, final Action0 onComplete)</throwable></p>
<p>这些订阅方法都会在内部构建一个Subscriber。<br>我们看到Action0 Action1… 等等，它们都是继承Action接口,只有一个call方法，不同在于Action0不需要参数，Action1一个参数…,其实这些Action就是代表subscriber的几个方法.我们就看一个源码。</p>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line">  <span class="keyword">public</span> <span class="keyword">final</span> <span class="function">Subscription <span class="title">subscribe</span><span class="params">(<span class="keyword">final</span> Action1&lt;? <span class="keyword">super</span> T&gt; onNext, <span class="keyword">final</span> Action1&lt;Throwable&gt; onError, <span class="keyword">final</span> Action0 onComplete)</span> </span>&#123;</div><div class="line"></div><div class="line"><span class="keyword">if</span> (onNext == <span class="keyword">null</span>) &#123;</div><div class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"onNext can not be null"</span>);</div><div class="line">&#125;</div><div class="line"><span class="keyword">if</span> (onError == <span class="keyword">null</span>) &#123;</div><div class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"onError can not be null"</span>);</div><div class="line">&#125;</div><div class="line"><span class="keyword">if</span> (onComplete == <span class="keyword">null</span>) &#123;</div><div class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"onComplete can not be null"</span>);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">return</span> <span class="title">subscribe</span><span class="params">(<span class="keyword">new</span> Subscriber&lt;T&gt;()</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="keyword">public</span> <span class="keyword">final</span> <span class="function"><span class="keyword">void</span> <span class="title">onCompleted</span><span class="params">()</span> </span>&#123;</div><div class="line">        onComplete.call();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="keyword">public</span> <span class="keyword">final</span> <span class="function"><span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable e)</span> </span>&#123;</div><div class="line">        onError.call(e);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="keyword">public</span> <span class="keyword">final</span> <span class="function"><span class="keyword">void</span> <span class="title">onNext</span><span class="params">(T args)</span> </span>&#123;</div><div class="line">        onNext.call(args);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">&#125;);</div></pre></td></tr></table></figure>
<p>我们看到其实在new Subscriber 这个接口的时候调用的都是传进来的Action。</p>
<h4 id="Rxjava的线程调度Scheduler"><a href="#Rxjava的线程调度Scheduler" class="headerlink" title="Rxjava的线程调度Scheduler"></a><a href="#Rxjava_u7684_u7EBF_u7A0B_u8C03_u5EA6Scheduler" title="Rxjava的线程调度Scheduler"></a>Rxjava的线程调度Scheduler</h4><p>在传统的回调中我们好自己去处理还代码运行的具体线程，是要开启新的线程，还是在UI线程中操作，我们都要New Thread() 或者使用Handler等等的方法去实现，其实挺麻烦的，那么Rxjava既然是处理异步的那么必定有自己的线程调度，我们看了解下Rxjava是怎么做的。</p>
<p>Rxjava默认会在调用者的线程去执行操作的。如果要制定线程去执行呢，Rxjava为我们提供了API,那就是Scheduler.</p>
<ul>
<li>Schedulers.immediate()在当前线程执行，即默认的</li>
<li>Schedulers.newThread()在新的线程执行</li>
<li>Schedulers.io()用于IO操作的线程</li>
<li>Schedulers.computation()用于图形的计算等线程</li>
<li>AndroidSchedulers.mainThread()这个RxAndroid库提供的用于UI操作</li>
</ul>
<p>Rxjava提供了2个方法来使用他们</p>
<ul>
<li>subscribeOn()指定事件产生的线程</li>
<li>observeOn()指定事件消费的线程</li>
</ul>
<p>我们来个例子</p>
<figure class="highlight haxe"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">List&lt;<span class="keyword">String</span>&gt; a = <span class="keyword">new</span> <span class="type">ArrayList</span>&lt;<span class="keyword">String</span>&gt;();</div><div class="line">a.add(<span class="string">"a"</span>);</div><div class="line">a.add(<span class="string">"b"</span>);</div><div class="line">a.add(<span class="string">"c"</span>);</div><div class="line">Observable.from(a).observeOn(Schedulers.<span class="keyword">new</span><span class="type">Thread</span>()).subscribeOn(AndroidSchedulers.mainThread()).subscribe(<span class="keyword">new</span> <span class="type">Action1</span>&lt;<span class="keyword">String</span>&gt;() &#123;</div><div class="line">    @Override</div><div class="line">    <span class="keyword">public</span> void call(<span class="keyword">String</span> s) &#123;</div><div class="line">        Log.e(<span class="string">"Joker"</span>, s);</div><div class="line">    &#125;</div><div class="line">&#125;);</div></pre></td></tr></table></figure>
<p>这里我们定义了事件产生在新的线程里，而事件消费在UI线程里。</p>
<h4 id="Operators"><a href="#Operators" class="headerlink" title="Operators"></a><a href="#Operators" title="Operators"></a>Operators</h4><p>我们在上面提到过Operators运算符。Rxjava为我们提供了很多的Operators。像上面的例子中得from就是一个运算符。所有的Operators我们可以看下<a href="http://reactivex.io/documentation/operators.html" target="_blank" rel="external">文档</a>。</p>
<p>我们按照Operators的分类来介绍常用的。</p>
<h5 id="Creating-Observables-可创建Observable："><a href="#Creating-Observables-可创建Observable：" class="headerlink" title="Creating Observables 可创建Observable："></a><a href="#Creating_Observables__u53EF_u521B_u5EFAObservable_uFF1A" title="Creating Observables 可创建Observable："></a>Creating Observables 可创建Observable：</h5><ul>
<li>from 可以从Iterable or Array中取出每个Item来创建一个Observable。</li>
</ul>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">Integer[] items = &#123; <span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span> &#125;;</div><div class="line">Observable myObservable = Observable.from(items);</div><div class="line"></div><div class="line">myObservable.subscribe(</div><div class="line">    <span class="keyword">new</span> Action1&lt;Integer&gt;() &#123;</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">call</span><span class="params">(Integer item)</span> </span>&#123;</div><div class="line">            System.out.println(item);</div><div class="line">        &#125;</div><div class="line">    &#125;,</div><div class="line">    <span class="keyword">new</span> Action1&lt;Throwable&gt;() &#123;</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">call</span><span class="params">(Throwable <span class="keyword">error</span>)</span> </span>&#123;</div><div class="line">            System.out.println(<span class="string">"Error encountered: "</span> + <span class="keyword">error</span>.getMessage());</div><div class="line">        &#125;</div><div class="line">    &#125;,</div><div class="line">    <span class="keyword">new</span> Action0() &#123;</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">call</span><span class="params">()</span> </span>&#123;</div><div class="line">            System.out.println(<span class="string">"Sequence complete"</span>);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">);</div><div class="line"></div><div class="line">结果如下</div><div class="line"><span class="number">0</span></div><div class="line"><span class="number">1</span></div><div class="line"><span class="number">2</span></div><div class="line"><span class="number">3</span></div><div class="line"><span class="number">4</span></div><div class="line"><span class="number">5</span></div><div class="line">Sequence complete</div></pre></td></tr></table></figure>
<ul>
<li><p>just：和from类似的，接受0-9个item作为参数去依次取出的一个Observable。</p>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">Observable.just(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>)</div><div class="line">  .subscribe(<span class="keyword">new</span> Subscriber&lt;Integer&gt;() &#123;</div><div class="line"><span class="meta">@Override</span></div><div class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">onNext</span><span class="params">(Integer item)</span> </span>&#123;</div><div class="line">    System.out.println(<span class="string">"Next: "</span> + item);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="meta">@Override</span></div><div class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable <span class="keyword">error</span>)</span> </span>&#123;</div><div class="line">    System.err.println(<span class="string">"Error: "</span> + <span class="keyword">error</span>.getMessage());</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="meta">@Override</span></div><div class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">onCompleted</span><span class="params">()</span> </span>&#123;</div><div class="line">    System.out.println(<span class="string">"Sequence complete."</span>);</div><div class="line">&#125;</div><div class="line">&#125;);</div><div class="line"></div><div class="line">结果如下：</div><div class="line">Next: 1</div><div class="line">Next: 2</div><div class="line">Next: 3</div><div class="line">Sequence complete.</div></pre></td></tr></table></figure>
</li>
<li><p>create：作为一个自定义的创建Observable，他可以在内部检查isUnsubscribed从而结束掉事件的处理，节约资源。</p>
</li>
</ul>
<figure class="highlight aspectj"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div></pre></td><td class="code"><pre><div class="line">Observable.create(<span class="keyword">new</span> Observable.OnSubscribe&lt;Integer&gt;() &#123;</div><div class="line"><span class="meta">@Override</span></div><div class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">call</span><span class="params">(Subscriber&lt;? <span class="keyword">super</span> Integer&gt; observer)</span> </span>&#123;</div><div class="line"><span class="keyword">try</span> &#123;</div><div class="line">    <span class="keyword">if</span> (!observer.isUnsubscribed()) &#123;</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; <span class="number">5</span>; i++) &#123;</div><div class="line">            observer.onNext(i);</div><div class="line">        &#125;</div><div class="line">        observer.onCompleted();</div><div class="line">    &#125;</div><div class="line">&#125; <span class="keyword">catch</span> (Exception e) &#123;</div><div class="line">    observer.onError(e);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"> &#125; ).subscribe(<span class="keyword">new</span> Subscriber&lt;Integer&gt;() &#123;</div><div class="line">    <span class="meta">@Override</span></div><div class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">onNext</span><span class="params">(Integer item)</span> </span>&#123;</div><div class="line">    System.out.println(<span class="string">"Next: "</span> + item);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="meta">@Override</span></div><div class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable <span class="keyword">error</span>)</span> </span>&#123;</div><div class="line">    System.err.println(<span class="string">"Error: "</span> + <span class="keyword">error</span>.getMessage());</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="meta">@Override</span></div><div class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">void</span> <span class="title">onCompleted</span><span class="params">()</span> </span>&#123;</div><div class="line">    System.out.println(<span class="string">"Sequence complete."</span>);</div><div class="line">&#125;</div><div class="line">&#125;);</div><div class="line"></div><div class="line">结果如下：</div><div class="line">Next: 1</div><div class="line">Next: 2</div><div class="line">Next: 3</div><div class="line">Next: 4</div><div class="line">Sequence complete.</div></pre></td></tr></table></figure>
<h5 id="Transforming-Observables-转换Observables"><a href="#Transforming-Observables-转换Observables" class="headerlink" title="Transforming Observables 转换Observables"></a><a href="#Transforming_Observables__u8F6C_u6362Observables" title="Transforming Observables 转换Observables"></a>Transforming Observables 转换Observables</h5><ul>
<li><p>map：通过一个函数将items中的每个item转换成一个Observable</p>
<figure class="highlight lasso"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">Observable.just(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>).<span class="built_in">map</span>(<span class="literal">new</span> Func1&lt;<span class="built_in">Integer</span>, <span class="built_in">Integer</span>&gt;() &#123;</div><div class="line">    @Override</div><div class="line">    <span class="keyword">public</span> <span class="built_in">Integer</span> call(<span class="built_in">Integer</span> <span class="built_in">integer</span>) &#123;</div><div class="line">       <span class="keyword">return</span> <span class="built_in">integer</span>*<span class="number">10</span>;</div><div class="line">    &#125;</div><div class="line">&#125;).subscribe(<span class="literal">new</span> Action1&lt;<span class="built_in">Integer</span>&gt;() &#123;</div><div class="line">    @Override</div><div class="line">    <span class="keyword">public</span> <span class="literal">void</span> call(<span class="built_in">Integer</span> <span class="built_in">integer</span>) &#123;</div><div class="line">        <span class="keyword">Log</span>.e(<span class="string">"Next"</span>,<span class="string">""</span>+<span class="built_in">integer</span>);</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line"></div><div class="line">结果如下：</div><div class="line">Next: <span class="number">10</span></div><div class="line">Next: <span class="number">20</span></div><div class="line">Next: <span class="number">30</span></div></pre></td></tr></table></figure>
</li>
</ul>
<p>-flatMap:通过一个函数将items转换成一个包含函数的Observables。<br>如果有一个数据结构是这样的</p>
<figure class="highlight mipsasm"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line">A a = new  ArrayList&lt;<span class="keyword">B&gt;(); </span>   </div><div class="line"><span class="keyword">b1 </span>=&#123;<span class="string">"a"</span>,<span class="string">"b"</span>,<span class="string">"c"</span>&#125;<span class="comment">;</span></div><div class="line">a = &#123;<span class="keyword">b1,b1,b1&#125;;</span></div><div class="line">如果我们要打印a中所有的<span class="keyword">b1的所以item。</span></div><div class="line">假如我们使用map的话：</div><div class="line">Observable.<span class="keyword">just(a,a,a).map(new </span>Func1&lt;A, <span class="keyword">B&gt;() </span>&#123;</div><div class="line">    @Override</div><div class="line">    public Integer call(<span class="keyword">B </span><span class="keyword">b) </span>&#123;</div><div class="line">       return <span class="keyword">b;</span></div><div class="line">    &#125;</div><div class="line">&#125;).<span class="keyword">subscribe(new </span>Action1&lt;<span class="keyword">B&gt;() </span>&#123;</div><div class="line">    @Override</div><div class="line">    public void call(<span class="keyword">B </span><span class="keyword">b) </span>&#123;</div><div class="line">        for(int i=<span class="number">0</span><span class="comment">;i&lt;b.size();i++)&#123;</span></div><div class="line">            Log.e(<span class="string">"Next"</span>,<span class="keyword">b.get(i));</span></div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;)<span class="comment">;</span></div><div class="line"></div><div class="line">但是for循环并不是我们想要的。那么我们可以使用flatMap：</div><div class="line"></div><div class="line">Observable.<span class="keyword">just(a,a,a).flatMap(new </span>Func1&lt;String, Observable&lt;<span class="keyword">B&gt;&gt;() </span>&#123;</div><div class="line">    @Override</div><div class="line">    public Observable&lt;<span class="keyword">B&gt; </span>call(<span class="keyword">B </span><span class="keyword">b) </span>&#123;</div><div class="line">        return  Observable.<span class="keyword">just(b);</span></div><div class="line">    &#125;</div><div class="line">&#125;).<span class="keyword">subscribe(new </span>Action1&lt;String&gt;() &#123;</div><div class="line">    @Override</div><div class="line">    public void call(String s) &#123;</div><div class="line">        Log.e(<span class="string">"Next"</span>,s)<span class="comment">;</span></div><div class="line">    &#125;</div><div class="line">&#125;)</div><div class="line">是不是便捷多了。</div></pre></td></tr></table></figure>
<h5 id="Combining-Observables-合并Observables"><a href="#Combining-Observables-合并Observables" class="headerlink" title="Combining Observables 合并Observables"></a><a href="#Combining_Observables__u5408_u5E76Observables" title="Combining Observables 合并Observables"></a>Combining Observables 合并Observables</h5><ul>
<li><p>join: 结合两个在规定时间段里的结合。</p>
<figure class="highlight haxe"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">private</span> Observable&lt;<span class="keyword">String</span>&gt; createObserver() &#123;</div><div class="line"><span class="keyword">return</span> Observable.create(<span class="keyword">new</span> <span class="type">Observable</span>.OnSubscribe&lt;<span class="keyword">String</span>&gt;() &#123;</div><div class="line">    @Override</div><div class="line">    <span class="keyword">public</span> void call(Subscriber&lt;? <span class="keyword">super</span> <span class="keyword">String</span>&gt; subscriber) &#123;</div><div class="line">        <span class="keyword">for</span> (int i = <span class="number">1</span>; i &lt; <span class="number">5</span>; i++) &#123;</div><div class="line">            subscriber.onNext(<span class="string">"Right-"</span> + i);</div><div class="line">            <span class="keyword">try</span> &#123;</div><div class="line">                Thread.sleep(<span class="number">1000</span>);</div><div class="line">            &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</div><div class="line">                e.printStackTrace();</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;).subscribeOn(Schedulers.<span class="keyword">new</span><span class="type">Thread</span>());</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
</ul>
<figure class="highlight vbscript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">private</span> Observable&lt;<span class="built_in">String</span>&gt; joinObserver() &#123;</div><div class="line">    return Observable.just(<span class="string">"Left-"</span>).<span class="built_in">join</span>(createObserver(),</div><div class="line">        integer -&gt; Observable.<span class="built_in">timer</span>(<span class="number">3000</span>, TimeUnit.MILLISECONDS),</div><div class="line">        integer -&gt; Observable.<span class="built_in">timer</span>(<span class="number">2000</span>, TimeUnit.MILLISECONDS),</div><div class="line">        (i, j) -&gt; i + j</div><div class="line">);</div><div class="line">&#125;</div><div class="line"></div><div class="line">joinObserver.subscribe(<span class="keyword">new</span> Action1&lt;<span class="built_in">String</span>&gt;() &#123;</div><div class="line">    @Override</div><div class="line">    <span class="keyword">public</span> void <span class="keyword">call</span>(<span class="built_in">String</span> s) &#123;</div><div class="line">        <span class="built_in">Log</span>.e(<span class="string">"Joker"</span>,s);</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line">结果如下:</div><div class="line">    <span class="built_in">Left</span>-<span class="built_in">Right</span><span class="number">-1</span></div><div class="line">    <span class="built_in">Left</span>-<span class="built_in">Right</span><span class="number">-2</span></div><div class="line">    <span class="built_in">Left</span>-<span class="built_in">Right</span><span class="number">-3</span></div></pre></td></tr></table></figure>
<p>好了我们就介绍这么多，Rxjava的操作符还有很多，我们可以参照下面大神的博客：<a href="http://blog.chinaunix.net/uid/20771867/list/2.html" target="_blank" rel="external">Operators介绍</a></p>

            <div class="post-copyright">
    <div class="content">
        <p>最后更新： 2017年09月15日 15:00</p>
        <p>原始链接： <a class="post-url" href="/2015/12/14/Rxjava-简化你的异步调用/" title="Rxjava-简化你的异步调用">http://mjjde.github.io/2015/12/14/Rxjava-简化你的异步调用/</a></p>
        <footer>
            <a href="http://mjjde.github.io">
                <img src="/images/avatar.jpg" alt="Joker Mei">
                Joker Mei
            </a>
        </footer>
    </div>
</div>

      
    </div>
    <footer class="article-footer">
        
        
<div class="post-share">
    <a href="javascript:;" id="share-sub" class="post-share-fab">
        <i class="fa fa-share-alt"></i>
    </a>
    <div class="post-share-list" id="share-list">
        <ul class="share-icons">
          <li>
            <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://mjjde.github.io/2015/12/14/Rxjava-简化你的异步调用/&title=《Rxjava-简化你的异步调用》 — 清风向阳&pic=http://mjjde.github.ioimages/avatar.jpg" data-title="微博">
              <i class="fa fa-weibo"></i>
            </a>
          </li>
          <li>
            <a class="weixin share-sns" id="wxFab" href="javascript:;" data-title="微信">
              <i class="fa fa-weixin"></i>
            </a>
          </li>
          <li>
            <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://mjjde.github.io/2015/12/14/Rxjava-简化你的异步调用/&title=《Rxjava-简化你的异步调用》 — 清风向阳&source=
Rxjava好似一股春风，刮到了各地的论坛，博客，其实Rxjava给我们带来的是一个对编程思维的改变，它改变了我们对异步回调的正常模式，主张链式是最具表..." data-title="QQ">
              <i class="fa fa-qq"></i>
            </a>
          </li>
          <li>
            <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://mjjde.github.io/2015/12/14/Rxjava-简化你的异步调用/" data-title="Facebook">
              <i class="fa fa-facebook"></i>
            </a>
          </li>
          <li>
            <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Rxjava-简化你的异步调用》 — 清风向阳&url=http://mjjde.github.io/2015/12/14/Rxjava-简化你的异步调用/&via=http://mjjde.github.io" data-title="Twitter">
              <i class="fa fa-twitter"></i>
            </a>
          </li>
          <li>
            <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://mjjde.github.io/2015/12/14/Rxjava-简化你的异步调用/" data-title="Google+">
              <i class="fa fa-google-plus"></i>
            </a>
          </li>
        </ul>
     </div>
</div>
<div class="post-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;" id="wxShare-close">×</a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://mjjde.github.io/2015/12/14/Rxjava-简化你的异步调用/" alt="微信分享二维码">
</div>

<div class="mask"></div>

        
        <ul class="article-footer-menu">
            
            
  <li class="article-footer-tags">
    <i class="fa fa-tags"></i>
      
    <a href="/tags/Rxjava/" class="color2">Rxjava</a>
      
  </li>

        </ul>
        
    </footer>
  </div>
</article>


    <aside class="post-toc-pos post-toc-top" id="post-toc">
        <nav class="post-toc-wrap">
            <ol class="post-toc"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Rxjava带给我们什么"><span class="post-toc-text">Rxjava带给我们什么</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#如何将添加Rxjava到项目中"><span class="post-toc-text">如何将添加Rxjava到项目中</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Rxjava的思想"><span class="post-toc-text">Rxjava的思想</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Rxjava的使用"><span class="post-toc-text">Rxjava的使用</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#观察者Observer"><span class="post-toc-text">观察者Observer</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#被观察者Observable"><span class="post-toc-text">被观察者Observable</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#订阅subscribe"><span class="post-toc-text">订阅subscribe</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Rxjava的线程调度Scheduler"><span class="post-toc-text">Rxjava的线程调度Scheduler</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Operators"><span class="post-toc-text">Operators</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#Creating-Observables-可创建Observable："><span class="post-toc-text">Creating Observables 可创建Observable：</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#Transforming-Observables-转换Observables"><span class="post-toc-text">Transforming Observables 转换Observables</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#Combining-Observables-合并Observables"><span class="post-toc-text">Combining Observables 合并Observables</span></a></li></ol></li></ol>
        </nav>
    </aside>
    

<nav id="article-nav">
  
    <a href="/2015/12/23/EventBus2-4-0源码分析之订阅/" id="article-nav-newer" class="article-nav-link-wrap">

      <span class="article-nav-title">
        <i class="fa fa-hand-o-left" aria-hidden="true"></i>
        
          EventBus2-4-0源码分析之订阅
        
      </span>
    </a>
  
  
    <a href="/2015/12/11/Volley-网络通讯框架二之处理请求/" id="article-nav-older" class="article-nav-link-wrap">
      <span class="article-nav-title">Volley-网络通讯框架二之处理请求</span>
      <i class="fa fa-hand-o-right" aria-hidden="true"></i>
    </a>
  
</nav>



    
</section>
        
      </div>
      <footer id="footer">
  <div class="outer">
    <div id="footer-info" class="inner">
      
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


      <p>
        Powered by  <a href="http://hexo.io/" target="_blank">Hexo</a>
        Theme <a href="//github.com/wongminho/hexo-theme-miho" target="_blank">MiHo</a>
      &copy; 2018 Joker Mei<br>
      </p>
    </div>
  </div>
</footer>
    <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<script src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
  var mihoConfig = {
      root: "http://mjjde.github.io",
      animate: false,
      isHome: false,
      share: true
  }
</script>
<div class="sidebar">
    <!-- <div id="sidebar-search" title="Search">
        <i class="fa fa-search"></i>
    </div> -->
    <div id="sidebar-category" title="Categories">
        <i class="fa fa-book"></i>
    </div>
    <div id="sidebar-tag" title="Tags">
        <i class="fa fa-tags"></i>
    </div>
    <div id="sidebar-top">
        <span class="sidebar-top-icon"><i class="fa fa-angle-up"></i></span>
    </div>
</div>
<div class="sidebar-menu-box" id="sidebar-menu-box">
    <div class="sidebar-menu-box-container">
        <div id="sidebar-menu-box-categories">
            <a class="category-link" href="/categories/Android技术/">Android技术</a><a class="category-link" href="/categories/其他技术/">其他技术</a><a class="category-link" href="/categories/网络技术/">网络技术</a><a class="category-link" href="/categories/设计模式/">设计模式</a>
        </div>
        <div id="sidebar-menu-box-tags">
            <a href="/tags/Activity/" style="font-size: 10px;">Activity</a> <a href="/tags/Android/" style="font-size: 16px;">Android</a> <a href="/tags/Android-IPC/" style="font-size: 10px;">Android-IPC</a> <a href="/tags/Binder/" style="font-size: 16px;">Binder</a> <a href="/tags/DiskLruCache/" style="font-size: 10px;">DiskLruCache</a> <a href="/tags/Fragment/" style="font-size: 10px;">Fragment</a> <a href="/tags/HTTP/" style="font-size: 20px;">HTTP</a> <a href="/tags/Java/" style="font-size: 10px;">Java</a> <a href="/tags/Notification/" style="font-size: 10px;">Notification</a> <a href="/tags/RemoteView/" style="font-size: 10px;">RemoteView</a> <a href="/tags/Rxjava/" style="font-size: 14px;">Rxjava</a> <a href="/tags/ServiceManager/" style="font-size: 14px;">ServiceManager</a> <a href="/tags/android/" style="font-size: 12px;">android</a> <a href="/tags/gradle/" style="font-size: 10px;">gradle</a> <a href="/tags/java/" style="font-size: 18px;">java</a> <a href="/tags/other/" style="font-size: 10px;">other</a> <a href="/tags/volley/" style="font-size: 12px;">volley</a> <a href="/tags/widget/" style="font-size: 10px;">widget</a> <a href="/tags/事件/" style="font-size: 10px;">事件</a> <a href="/tags/组件化/" style="font-size: 10px;">组件化</a> <a href="/tags/设计模式/" style="font-size: 18px;">设计模式</a>
        </div>
    </div>
    <a href="javascript:;" class="sidebar-menu-box-close">&times;</a>
</div>
<div class="mobile-header-menu-nav" id="mobile-header-menu-nav">
    <div id="mobile-header-menu-container">
        <span class="title">Menu</span>
        <ul class="mobile-header-menu-navbar">
            
            <li>
                <a  href="/">
                    <i class="fa fa-home"></i><span>Home</span>
                </a>
            </li>
            
            <li>
                <a  href="/archives">
                    <i class="fa fa-archive"></i><span>Archives</span>
                </a>
            </li>
            
            <li>
                <a  href="/about">
                    <i class="fa fa-user"></i><span>About</span>
                </a>
            </li>
            
        </ul>
    </div>
</div>
<div class="search-wrap">
    <span class="search-close">&times;</span>
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
            <i class="icon icon-lg icon-chevron-left"></i>
        </a>
        <input class="search-field" placeholder="Search..." id="keywords">
        <a id="search-submit" href="javascript:;">
            <i class="fa fa-search"></i>
        </a>
    <div class="search-container" id="search-container">
        <ul class="search-result" id="search-result">
        </ul>
    </div>
</div>

<div id="search-tpl">
    <li class="search-result-item">
        <a href="{url}" class="search-item-li">
            <span class="search-item-li-title" title="{title}">{title}</span>
        </a>
    </li>
</div>
<script src="/js/search.js"></script>
<script src="/js/main.js"></script>








  </div>
</body>
</html>